﻿
using Newtonsoft.Json;
using RTW.OA.Areas.Common.Servers;
using RTW.OA.Models;
using RTW.Utils.Utils;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace RTW.OA.Areas.Common.Controllers
{
    public class AssetController : Controller
    {
        private AssetServers assetServers;
        private AssetTypeServers assettypeServers;
        private AssetKeepServers assetkeepServers;
        public AssetController()
        {
            assetServers = new AssetServers();
            assettypeServers = new AssetTypeServers();
            assetkeepServers = new AssetKeepServers();
        }


        // GET: Common/Asset
        public ActionResult Index()
        {

            return View();
        }

        // GET: Common/Asset/Edit/5
        public ActionResult Edit(int? id)
        {
            ViewBag.TYPE_IDData = assetServers.GetTYPE_ID();
            ViewBag.DEPT_IDData = assetServers.GetDEPT_ID();
            ViewBag.CPTL_KINDData = assetServers.GetCPTL_KIND();
            ViewBag.PRCS_IDData = assetServers.GetPRCS_ID();
            var model = new asset_reflect();
            if (id != null)
            {
                ViewBag.Title = "编辑资产表";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建资产表";
            }
            return View(model);
        }

        // GET: Common/Asset/Edit/5
        public ActionResult Details(int? id)
        {
            ViewBag.TYPE_IDData = assetServers.GetTYPE_ID();
            ViewBag.DEPT_IDData = assetServers.GetDEPT_ID();
            ViewBag.CPTL_KINDData = assetServers.GetCPTL_KIND();
            ViewBag.PRCS_IDData = assetServers.GetPRCS_ID();
            var model = new asset_reflect();
            if (id != null)
            {
                ViewBag.Title = "资产明细";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建资产表";
            }
            return View(model);
        }

        // POST: Common/Asset/Edit/5
        [HttpPost]
        public ActionResult Edit(int? id, asset_reflect model)
        {
            ViewBag.TYPE_IDData = assetServers.GetTYPE_ID();
            ViewBag.DEPT_IDData = assetServers.GetDEPT_ID();
            ViewBag.CPTL_KINDData = assetServers.GetCPTL_KIND();
            ViewBag.PRCS_IDData = assetServers.GetPRCS_ID();
            try
            {
                if (id == null || id == 0)
                { 
                    assetServers.Add(model);       
                    ViewBag.msg = "新增资产表信息成功！";
                    model = new asset_reflect();

                } 
                else
                {
                    assetServers.Update(model);
                    ViewBag.msg = "修改资产表信息成功！";

                }
            }
            catch
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        // GET: Common/Asset/Delete/5
        public ActionResult Delete(int id)
        {
            if (id != 0)
            {
                assetServers.Del(id.ToString());
                ViewBag.msg = "删除资产表信息成功！";
            }
            return Content("");
        }


        // GET: Common/AssetType
        public ActionResult AssetType()
        {

            return View();
        }

        // GET: Common/AssetType/Edit/5
        public ActionResult AssetTypeEdit(int? id)
        {
            var model = new asset_type();
            if (id != null)
            {
                ViewBag.Title = "编辑资产类别表";
                model = GetAssetTypeModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建资产类别表";
            }
            return View(model);
        }

        // POST: Common/AssetType/Edit/5
        [HttpPost]
        public ActionResult AssetTypeEdit(int? id, asset_type model)
        {
            try
            {
                if (id == null || id == 0)
                {
                    assettypeServers.Add(model);
                    ViewBag.msg = "新增资产类别表信息成功！";
                    model = new asset_type();

                }
                else
                {
                    assettypeServers.Update(model);
                    ViewBag.msg = "修改资产类别表信息成功！";

                }
            }
            catch 
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        // GET: Common/AssetType/Delete/5
        public ActionResult AssetTypeDelete(int id)
        {
            if (id != 0)
            {
                assettypeServers.Del(id.ToString());
                ViewBag.msg = "删除资产类别表信息成功！";
            }
            return Content("");
        }

        // GET: Common/AssetKeep/Edit/5
        public ActionResult AssetKeepEdit(int? id)
        {
            ViewBag.KEEP_TYPEData = assetkeepServers.GetKEEP_TYPE();
            var model = new asset_keep();
            if (id != null)
            {
                ViewBag.Title = "编辑资产维护表";
                model = GetAssetKeepModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建资产维护表";
            }
            return View(model);
        }

        // POST: Common/AssetKeep/Edit/5
        [HttpPost]
        public ActionResult AssetKeepEdit(int? id, asset_keep model)
        {
            ViewBag.KEEP_TYPEData = assetkeepServers.GetKEEP_TYPE();
            try
            {
                //if (id == null || id == 0)
                if(1==1)
                {
                    model.CPTL_ID = id.ToString();
                    assetkeepServers.Add(model);
                    ViewBag.msg = "新增资产维护表信息成功！";
                    model = new asset_keep();

                }
                else
                {
                    //assetkeepServers.Update(model);
                    //ViewBag.msg = "修改资产维护表信息成功！";

                }
            }
            catch 
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        // GET: Common/AssetKeep/Edit/5
        public ActionResult Decrease(int? id)
        {
            ViewBag.DCR_PRCS_IDData = assetServers.DCR_PRCS_IDData();
            var model = new asset_reflect();
            if (id != null)
            {
                ViewBag.Title = "编辑资产维护表";
                model = assetServers.GetModelById(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建资产维护表";
            }
            return View(model);
        }

        // POST: Common/AssetKeep/Edit/5
        [HttpPost]
        public ActionResult Decrease(int? id, asset_reflect model)
        {
            ViewBag.DCR_PRCS_IDData = assetServers.DCR_PRCS_IDData();
            try
            {
                if (id == null || id == 0)
                {
                    //assetkeepServers.Add(model);
                    //ViewBag.msg = "新增资产维护表信息成功！";
                    //model = new asset_reflect();

                }
                else
                {
                    var m = assetServers.GetModelById(id.Value.ToString());
                    m.DCR_DATE = DateTime.Now;
                    m.DCR_PRCS_ID = model.DCR_PRCS_ID;
                    assetServers.Update(m);
                    ViewBag.msg = "修改资产维护表信息成功！";

                }
            }
            catch 
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        #region //GetData
        public string GetTableJsonResult(
                                 )
        {
            var list = assetServers.GetData();
            return JsonConvert.SerializeObject(list);
        }

        public asset_reflect GetModel(string id)
        {
            var model = assetServers.GetModelById(id);
            return model;
        }

        public string GetAssetTypeTableJsonResult()
        {
            var list = assettypeServers.GetData();
            return JsonConvert.SerializeObject(list);
        }

        public asset_type GetAssetTypeModel(string id)
        {
            var model = assettypeServers.GetModelById(id);
            return model;
        }
        public asset_keep GetAssetKeepModel(string id)
        {
            var model = assetkeepServers.GetModelById(id);
            return model;
        }


        #endregion //GetData
    }
}